<?php
include_once $_SERVER['DOCUMENT_ROOT'].'/phorm/database/core/DatabaseColumn.php';
include_once $_SERVER['DOCUMENT_ROOT'].'/phorm/database/core/IDatabaseRecord.php';
include_once $_SERVER['DOCUMENT_ROOT'].'/phorm/database/core/IJoinableColumn.php';

class ForeignKey extends DatabaseColumn implements IJoinableColumn
{	
	public $relation;
	public $to_field;
	public $on_delete;
	public $on_update;
	//public $value;
	
	public function __construct(IDatabaseRecord &$object, $class, $on_delete = 'CASCADE', $on_update = NULL, $field_type = 'int', $length = 11, $html_field = null, $to_field = null)
	{
		// previoulsy was using debug_backtrace()
		// but we found an issue when (we think) php was running as CGI.  The ['object']
		// key was not being passed.
		// signature was:
		// public function __construct($length = null, $defaultValue = null)
		// $this->model was being set via:
		// $tmp =  debug_backtrace();
		// $this->model        = &$tmp[1]['object'];
		
		$this->model         = $object;
		//$this->length        = $length;
		//$this->field_type    = $field_type;
		$this->to_field      = $to_field;
		$this->html_field    = $html_field;
		$this->on_delete     = $on_delete;
		$this->on_update     = $on_update;
		
		$c = new $class();
		$this->field_type = $c->primary_key->field_type;
		$this->length     = $c->primary_key->length;
		$c->release();
		unset($c);
		
		try
		{
			if ($class !== get_class($this->model))
				$this->relation = new $class();
			else
				throw new Exception("Fatal: ForeignKey in ".get_class($this->model)." cannot point to itself.");
		}
		catch (Exception $e)
		{
			echo $e->getMessage();
			exit;
		}
	}
	
	public function add(IDatabaseRecord &$object)
	{
		$this->relation = $object;
		
		if (!$object->primary_key->value)
			$object->save();
		
		$this->data['value'] = $object->primary_key->value;
	}
	
	public function __toString()
	{
		return $this->data['value'];
	}
	
	public function __get($property)
	{	
		if ($this->relation->$property) 
			return  $this->relation->$property;
		else
			return $this->data['value'];
	}
	
	public function __destruct()
	{
		$this->relation->release();
		parent::__destruct();
	}
	
	/*public function toHTML()
	{
		$value = @$this->data['value'];
		$html  = "<tr class=\"phorm-toHTML-row\">\n\t";
		$html .= "<td class=\"phorm-toHTML-label\" align=\"right\">".$this->column.": </td>\n\t";
		$html .= "<td class=\"phorm-toHTML-input\">\n\t";
		$html .= "<select class=\"phorm-toHTML-select\" name=\"{$this->column}\" value=\"{$value}\">\n\t";
		
		$records = $this->relation->records();
		if ($records->length)
		{
			foreach($records as $value)
			{
				if (isset($this->data['value']) &&  $value->primary_key->value == $this->data['value'])
					$html .="<option value=\"{$value->primary_key->value}\" selected=\"true\">{$value->{$this->html_field}}</option>\n\t";
				else
					$html .="<option value=\"{$value->primary_key->value}\">{$value->{$this->html_field}}</option>\n\t";
			}
		}
		
		$html .= "</select>\n";
		$html .= "<tr>\n";
		
		return $html;
	}*/
	
}
?>
